package com.demo.maintenance.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.demo.maintenance.R;
import com.demo.maintenance.bean.BaseResponse;
import com.demo.maintenance.bean.ChildInfoResponse;
import com.demo.maintenance.contant.Global;
import com.demo.maintenance.utils.AntiShakeUtils;
import com.demo.maintenance.utils.HttpManager;
import com.demo.maintenance.utils.SystemBarTintManager;
import com.hjq.toast.ToastUtils;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AssignUploadActivity extends AppCompatActivity {

    private TextView btn_1;

    private EditText et_2;

    private EditText et_3;

    private List<ChildInfoResponse.Data> childInfoList = new ArrayList<>();

    private long courseId = -1;
    private long childId = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        QMUIStatusBarHelper.translucent(this);
        SystemBarTintManager.setStatusBarDarkTheme(this, true);
        setContentView(R.layout.activity_upload_assignment);

        findViewById(R.id.common_back_iv).setOnClickListener(v -> finish());
        ((TextView) findViewById(R.id.common_content_tv)).setText("Upload Assignment");

        btn_1 = findViewById(R.id.btn_1);
        btn_1.setOnClickListener(v -> {
            if (AntiShakeUtils.isInvalidClick(v)){
                return;
            }
            uploadChildStatus();
        });

        et_2 = findViewById(R.id.et_2);
        et_3 = findViewById(R.id.et_3);

        if (getIntent() != null){
            courseId = getIntent().getLongExtra("courseId", -1);
            childId = getIntent().getLongExtra("childId", -1);
        }

    }

    /**
     * upload child status
     */
    private void uploadChildStatus() {

        if (TextUtils.isEmpty(et_2.getText().toString())
                || TextUtils.isEmpty(et_3.getText().toString())){
            ToastUtils.show("please input");
            return;
        }

        WaitDialog.show("Uploading");

        String url = Global.ipAddress + "/system/assignment";
        Map<String, String> params = new HashMap<>();
        params.put("childId", String.valueOf(childId));
        params.put("teacherId", String.valueOf(Global.userResponse.data.userId));
        params.put("createBy", Global.userResponse.data.userName);
        params.put("assignmentName", et_2.getText().toString());
        params.put("assignmentDesc", et_3.getText().toString());
        params.put("courseId", String.valueOf(courseId));

        HttpManager.postJson(url, params, new HttpManager.NetworkCallBack() {
            @Override
            public void onSuccess(String successInfo) {
                try{
                    Log.e("monitoring", successInfo);

                    BaseResponse response = JSON.parseObject(successInfo, BaseResponse.class);
                    if (response.code == 200){
                        runOnUiThread(() -> {
                            ToastUtils.show("Upload Success");
                            finish();
                        });
                    }else {
                        Log.e("monitoring", "failed !");

                        runOnUiThread(() -> {
                            ToastUtils.show("Upload Failed");
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();

                    runOnUiThread(() -> {
                        ToastUtils.show("Upload Failed");
                    });
                }
            }

            @Override
            public void onError(String errorInfo) {
                Log.e("monitoring", errorInfo);
                runOnUiThread(() -> {
                    ToastUtils.show("Upload Failed");
                });
            }

            @Override
            public void onFinal() {
                super.onFinal();
                WaitDialog.dismiss();
            }
        });
    }



}
